<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" action="#" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">
							<h id="star">* </h>配送单位</td>
						<td width="30%">
							<input class="easyui-combobox" style="width: 100%" type="text" name="CUSTOMER_NAME" id="input_CUSTOMER_NAME" />
						</td>
						<td width="20%">
							<h id="star">* </h>用户组</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 100%;" type="text" name="CUSTOMER_TYPE" id="input_CUSTOMER_TYPE"></input>
						</td>
						<td style="display: none;">
							<input class="easyui-textbox" style="width: 100%;" type="text" name="ID" id="input_ID"></input>
						</td>
					</tr>
					<tr>
						<td>
							<h id="star">* </h>订单编号</td>
						<td>
							<input class="easyui-textbox" style="width: 100%;" type="text" name="ORDER_NO" id="input_ORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true"></input>
						</td>
						<td>
							<h id="star">* </h>配送时间</td>
						<td>
							<input class="easyui-datebox" style="width:100%;" type="text" name="DISTRIBUTION_DATE" id="input_DISTRIBUTION_DATE" data-options="required:true" />
						</td>
					</tr>
					<tr id="nodisplay" style="display: none">
						<td>
							<h id="star">* </h>用户组ID</td>
						<td>
							<input class="easyui-textbox" style="width:100%;" type="text" name="CUSTOMERTYPE_ID" id="input_CUSTOMERTYPE_ID" data-options="prompt:'由系统自动生成，不需填写',disabled:true," />
						</td>
						<td width="20%">
							<h id="star">* </h>配送单位编号</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 100%;" type="text" name="CUSTOMER_ID" id="input_CUSTOMER_ID" data-options="required:true,disabled:true" />
						</td>
					</tr>
				</table>
				<table id="tt" style="width: 100%;">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input class="easyui-textbox" type="text" id="input_LUMP_SUM" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" class="easyui-textbox" id="input_LUMP_SUM_CHINESE" />
							</td>
						</tr>
					</table>
				</div>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/finance.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
	<script type="text/javascript" src="EventUtil.js"></script>
	<script type="text/javascript" src="util.js"></script>
	<script type="text/javascript" src="OrderHandle.js"></script>
	<script type="text/javascript" src="OrderClass.js"></script>
	<script type="text/javascript" src="ToolBar.js"></script>
	<script type="text/javascript" src="../../../JS/addRowShareFunction.js"></script>
	<script type="text/javascript">
		function OriginOrderData() {
			var specialDataArr=["CUSTOMERTYPE_ID","CUSTOMER_ID","CUSTOMER_NAME","CUSTOMER_TYPE","DISTRIBUTION_DATE","ISVERIFY","rows","ISWEB","LUMP_SUM"];
			specialDataArr=specialDataArr.concat(this.COMMON_INPUT_DATA_ARR);
		    this.inputDataArr=specialDataArr;//获取的数据字段
		    this.addDataArr=["CUSTOMERTYPE_ID","CUSTOMER_ID","DISTRIBUTION_DATE","rows","LUMP_SUM","MEMO"];
		    this.updateDataArr=["ORDER_NO","ID","CUSTOMERTYPE_ID","CUSTOMER_ID","DISTRIBUTION_DATE","rows","LUMP_SUM","MEMO"];
			this.save = function() {
				console.log(this);
			};
			this.getRemoteData = function(_url, type) {
				var handle = new OrderHandle();
				var res = handle.getRemoteData(_url);
				var that = this;
				res.done(function(data) {
					if(data != null) {
						that.getData(data);
						that.displayPageData();
						verifyStatus(that.ISVERIFY, true);
						$('#tt').edatagrid('loadData', that.rows);
					}
				});
			};
		}
		extend(OriginOrderData, OrderAbstract);
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var signalOrderData = new OriginOrderData();
		var _detailToolbarData;
		var updateLog = "";
		var currRow;
		addFooter($("#new_footer"));
		var receipts_type = RECEIPT_TOOLBAR.FRONT;
		receipts_type += RECEIPT_TOOLBAR.NEXT;
		receipts_type += RECEIPT_TOOLBAR.ADD;
		receipts_type += RECEIPT_TOOLBAR.SAVE;
		receipts_type += RECEIPT_TOOLBAR.VERIFY;
		receipts_type += RECEIPT_TOOLBAR.UNVERIFY;
		try {
			signalOrderData.APIURL = RemoteLinkAddress + "/logistics/salesorder";
			signalOrderData.getMaxAndMin();
			var orderReceiptsToolBar = new ReceiptsToolBar(receipts_type);
			var content = orderReceiptsToolBar.processHtml();
			$("#toolbar").append(content);
			signalOrderData.addCommondEventListen();
		} catch(e) {
			$.messager.alert("错误", e.message);
		}
		_detailToolbarData = AddDetailToolBar(7);
		GetDetailTable(signalOrderData);
		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'INTERNALTRADE_ID');
		
		console.log(_detailToolbarData);
		$(document).ready(function() {
			if(id != AddFlag && id != undefined) {
				signalOrderData.ID = id;
				signalOrderData.loadDataByID();
			} else {
				signalOrderData.ID = -1;
			}
		});

		$('#input_CUSTOMER_NAME').combobox({
			url: RemoteLinkAddress + "/logistics/customer/listall.do",
			valueField: 'CUSTOMER_NAME',
			textField: 'CUSTOMER_NAME',
			panelWidth: 200,
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(!isEnglish(newValue)) {
					return;
				}
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/customer/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) { //当选中数据后调用该function，并得到该combobox对应的valuefield和textfield，赋值给record
				$('#input_CUSTOMER_ID').textbox('setValue', record.ID);
				$('#input_CUSTOMERTYPE_ID').textbox('setValue', record.TYPE_ID);
				$('#input_CUSTOMER_TYPE').textbox('setValue', record.CUSTOMER_TYPE);
			}, //调用easyui的textbox的写入值方法，将record中name为CUSTOMER_ID的写入id为customer_id的textbox中
		});

		function newdata() { //新建
			//			signalOrderData={};
			var s = new Object()
			s.ID = -1;
			s.ISVERIFY = 0;
			s.ISWEB = 0;
			signalOrderData.getData(s);
			console.log(signalOrderData);
			$("#input_CUSTOMER_NAME").textbox("setValue", "");
			$("#input_CUSTOMER_TYPE").textbox("setValue", "");
			$("#input_ORDER_NO").textbox("setValue", "");
			$("#input_DISTRIBUTION_DATE").datebox("setValue", "");
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
			$('#tt').edatagrid("iniRow");
			$("#notreview").empty();
		}

		function save() { //保存
			try {
				var data = submitFormData();
				if(signalOrderData.ID != -1) {
					signalOrderData.LOG = updateLog;
				} else {
					var dataGridRows = $("#tt").datagrid("getRows");
					data.rows = dataGridRows;
				}
				signalOrderData.getData(data);
				signalOrderData.checkData();
				submitData(signalOrderData);
			} catch(e) {
				$.messager.alert("错误", e.message);
			}
		}

		function searchmore() { //查询
			AddTabIn("原始订单查询", parent.$('#tabs'), "UI/LOGISTICS/OriginalOrder/OriginalOrderSearch.html");
		}

		function loadRemote(id) { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/logistics/originalorder/findbyid.do?ID=' + id;
			getRemoteData(_url);
		}

		function GetDetailTable(updateData) { //构造datagrid中的数据
			$('#tt').edatagrid({
				height: 310,
				toolbar: _detailToolbarData, //toolbar选择自定义的名为toolbar的工具栏
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center',
					}, {
						field: 'INTERNALTRADE_ID',
						title: '内部商品ID',
						width: '16%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "INTERNALTRADE_ID",
								textField: "INTERNALTRADE_ID",
								editable: false,
								required: true
							}
						}
					}, {
						field: 'INTERNAL_NAME',
						title: '*内部商品名',
						width: '16%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: 'INTERNAL_NAME',
								textField: 'INTERNAL_NAME',
								panelWidth: 200,
								panelHeight: 'auto',
								mode: 'remote',
								method: 'get',
								onChange: function(newValue, oldValue) {
									if(!isEnglish(newValue)) {
										return;
									}
									if(newValue != "" && newValue != undefined) {
										var url = RemoteLinkAddress + "/logistics/internaltrade/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
										$(this).combobox("reload", url);
									}
								},
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'UNIT_NAME'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.UNIT_NAME);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'INTERNALTRADE_ID'
									}).target;
									target.textbox('clear');
									if(record.ID != undefined) {
										target.textbox('setValue', record.ID);
									}
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'INTERNAL_TYPE'
									}).target;

									target.textbox('clear');
									if(record.INTERNAL_TYPE != undefined) {
										target.textbox('setValue', record.INTERNAL_TYPE);
									}
								}
							}
						},
					}, {
						field: 'INTERNAL_TYPE',
						title: '*内部商品类型',
						width: '16%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "INTERNAL_TYPE",
								textField: "INTERNAL_TYPE",
								editable: true,
								required: true
							}
						}
					}, {
						field: 'UNIT_NAME',
						title: '*单位',
						width: '16%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "UNIT_NAME",
								textField: "UNIT_NAME",
							}
						}
					}, {
						field: 'AMOUNT',
						title: '*数量',
						width: '15%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
								precision: 2,
								groupSeparator: ',',
								decimalSeparator: '.',
							}
						}
					}, {
						field: 'UNIT_PRICE',
						title: '*单价',
						width: '16%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								precision: 2, //保留两位
								groupSeparator: ',', //每组用','隔开
								decimalSeparator: '.',
								prefix: '￥' //在数据前面加￥符号
							}
						},
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}, {
						field: 'SUM',
						title: '*总价',
						width: '15%',
						align: 'center',
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					var amount = row.AMOUNT; //将该行name为AMOUNT的数据赋值给amount
					var perprice = row.UNIT_PRICE; //同上
					var result = amount * perprice; //将上述两个的值相乘再复制给result
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							SUM: result.toFixed(2),
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					CalSum();
					///当修改时触发此
					if(updateData.ID != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											var tmp_updatelog = "[修改记录]:[品种]" + row.INTERNAL_NAME + "[" + searchOrderFieldMap(s) + "] 原值为:" + obj[s] + " 新值为:" + row[s] + ";";
											updateLog += tmp_updatelog;
											obj[s] = row[s];
										}
									}
								}
							}
						} else {
							var len = updateData.rows.length;
							updateData.rows[len] = row;
							updateData.rows[len].ORDER_ID = updateData.ID;
							var addItem = logProcess(updateData.rows[len], "add");
							updateLog += addItem;
						}
					}
				},
				onDestroy: function(index, row) {
					if(currid != AddFlag) {
						for(var i = 0; i < updateData.rows.length; i++) {
							if(updateData.rows[i].ID == row.ID) {
								updateData.rows[i].ORDER_ID = ""; //直接替换
								var removeItem = logProcess(row, "delete");
								updateLog += removeItem;
							}
						}
					}
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				}
			});
		}

		function getUpdateData() {
			var data = submitFormData();
			signalOrderData.getData(data);
			signalOrderData.LOG = updateLog;
			submitData(signalOrderData);
		}

		function getSaveData() {
			var data = Object();
			data = submitFormData();
			var dataGridRows = $("#tt").datagrid("getRows");
			data.rows = dataGridRows;
			signalOrderData.ID = -1;
			submitData(signalOrderData);
		}

		function submitData(_data) {
			var _url = "";
			if(signalOrderData.ID == AddFlag) {
				_url = RemoteLinkAddress + "/logistics/originalorder/save.do";
			} else {
				_url = RemoteLinkAddress + "/logistics/originalorder/update.do";
			}
			var _senddata = JSON.stringify(_data);
			lockButton("lock");
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					lockButton("unlock");
					if(currid == AddFlag) {
						var revData = result.msg.split(";");
						signalOrderData.ID = revData[1];
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						loadRemote(signalOrderData.ID);
						$.messager.alert("提示", "新建保存成功");
						return;
					} else {
						loadRemote();
						$.messager.alert("提示", "修改保存成功");
					}
				},
				error: function(result) {
					$.messager.alert("错误", "保存不成功");
				}
			});
		}

		function CalSum() {
			getFormData(signalOrderData);
		}

		function checkDataIntegrity() {
			var distribution = $("input[name='CUSTOMER_NAME']").val();
			var usergroup = $("input[name='CUSTOMER_TYPE']").val();
			var orderno = $("input[name='ORDER_NO']").val();
			var distributiondata = $("input[name='DISTRIBUTION_DATE']").val();
			var rowsdata = $("#tt").datagrid('getRows');
			if(distribution != "" && usergroup != "" && orderno != "" && distributiondata != "") {
				if(rowsdata.length != 0) {
					for(var i = 0; i < rowsdata.length; i++) {
						var internaltype = rowsdata[i].INTERNAL_TYPE;
						var internalname = rowsdata[i].INTERNAL_NAME;
						var unitname = rowsdata[i].UNIT_NAME;
						var unitprice = rowsdata[i].UNIT_PRICE;
						var amount = rowsdata[i].AMOUNT;
						var sum = rowsdata[i].SUM;
						if(internalname == "" || internaltype == "" || unitname == "" || amount == "") {
							return 0;
						}
					}
				} else {
					return 0;
				}
			} else {
				return 0;
			}
		}

		function exportExcel() {
			if(isVerify == 0) {
				$.messager.alert("警告", "订单未审核，不能导出");
				return;
			}
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": _dataPage.ID
						},
						url: RemoteLinkAddress + "/logistics/originalorder/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
							//							this.onClick(location = result.path);
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}
	</script>

</html>